CC := $(CROSS_COMPILE)gcc

PROJECT_DIR := $(shell pwd)

CFLAGS += -I$(PROJECT_DIR) -Wall -Wextra -lpaho-mqtt3c -lcurl -lcrypto
ifdef CROSS_COMPILE
CFLAGS += --sysroot=$(SYSROOT)
endif

TARGET := gateway

PEER = root@192.168.46.31

# 普通源文件
SRCS += $(shell find app -type f -name "*.c")
SRCS += $(shell find daemon -type f -name "*.c")
SRCS += $(shell find ota -type f -name "*.c")
SRCS += $(shell find thirdparty -type f -name "*.c")

# 测试源文件
TEST_SRCS += $(shell find test -type f -name "*.c")
TEST_SRCS += $(SRCS)

OBJS := $(SRCS:.c=.o)
TEST_OBJS := $(TEST_SRCS:.c=.o)

.PHONY: all clean test cross

all: $(TARGET)

cross: 
	CROSS_COMPILE=/home/skiinder/atguigupi/toolchain/bin/arm-linux-gnueabihf- \
	SYSROOT=/home/skiinder/atguigupi/buildroot/output/staging \
	make
	@scp $(TARGET) $(PEER):/usr/bin/$(TARGET)
	@scp init.d/S99gateway $(PEER):/etc/init.d/S99gateway

$(TARGET): $(OBJS) main.o
	-@$(CC) -o $@ $^ $(CFLAGS)

clean:
	-@$(RM) $(TARGET) $(OBJS) main.o test.o

test: $(TEST_OBJS) test.o
	-@$(CC) -o test_exe $^ $(CFLAGS)
	-@./test_exe
	-@$(RM) test_exe $^